/////////////////////////////////////////////////////////////////////////////
// Name:        ax_containerctrl.cpp
// Purpose:     Contains the main elements of the UI
// Author:      Julian Smart
// Modified by: 
// Created:     22/03/2009 10:08:34
// RCS-ID:      
// Copyright:   
// Licence:     New BSD License
/////////////////////////////////////////////////////////////////////////////

#include "wx/wx.h"

////@begin includes
////@end includes

#include "ax/ax_containerctrl.h"
#include "ax/ax_controller_standard.h"
#include "ax/ax_utils.h"
#include "ax/ax_view.h"

////@begin XPM images
////@end XPM images


/*
 * AxContainerCtrl type definition
 */

IMPLEMENT_DYNAMIC_CLASS( AxContainerCtrl, wxPanel )


/*
 * AxContainerCtrl event table definition
 */

BEGIN_EVENT_TABLE( AxContainerCtrl, wxPanel )
    EVT_SYS_COLOUR_CHANGED(AxContainerCtrl::OnSysColourChanged)

////@begin AxContainerCtrl event table entries
    EVT_IDLE( AxContainerCtrl::OnIdle )

    EVT_LIST_KEY_DOWN( wxID_ANY, AxContainerCtrl::OnAnyKeyDown )

    EVT_BUTTON( ID_UP, AxContainerCtrl::OnUpClick )
    EVT_UPDATE_UI( ID_UP, AxContainerCtrl::OnUpUpdate )

    EVT_BUTTON( ID_HOME, AxContainerCtrl::OnHomeClick )
    EVT_UPDATE_UI( ID_HOME, AxContainerCtrl::OnHomeUpdate )

////@end AxContainerCtrl event table entries

END_EVENT_TABLE()


/*
 * AxContainerCtrl constructors
 */

AxContainerCtrl::AxContainerCtrl()
{
    Init();
}

AxContainerCtrl::AxContainerCtrl( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create(parent, id, pos, size, style);
}


/*
 * AxContainerCtrl creator
 */

bool AxContainerCtrl::Create( wxWindow* parent, wxWindowID id, const wxPoint& pos, const wxSize& size, long style )
{
////@begin AxContainerCtrl creation
    wxPanel::Create( parent, id, pos, size, style );

    CreateControls();
    Centre();
////@end AxContainerCtrl creation
    return true;
}


/*
 * AxContainerCtrl destructor
 */

AxContainerCtrl::~AxContainerCtrl()
{
    AxMenu::DoDelayedMenuDeletion();

////@begin AxContainerCtrl destruction
////@end AxContainerCtrl destruction
}


/*
 * Member initialisation
 */

void AxContainerCtrl::Init()
{
    m_controller = NULL;

////@begin AxContainerCtrl member initialisation
    m_titleCtrl = NULL;
    m_menuCtrl = NULL;
    m_pagerCtrl = NULL;
    m_descriptionCtrl = NULL;
////@end AxContainerCtrl member initialisation
}


/*
 * Control creation for AxContainerCtrl
 */

void AxContainerCtrl::CreateControls()
{    
////@begin AxContainerCtrl content construction
    AxContainerCtrl* itemPanel1 = this;

    wxBoxSizer* itemBoxSizer2 = new wxBoxSizer(wxVERTICAL);
    itemPanel1->SetSizer(itemBoxSizer2);

    wxBoxSizer* itemBoxSizer3 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer2->Add(itemBoxSizer3, 1, wxGROW, 5);

    wxBoxSizer* itemBoxSizer4 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer4, 0, wxGROW, 5);

    wxStaticText* itemStaticText5 = new wxStaticText( itemPanel1, wxID_STATIC, _("&Title:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemStaticText5->Show(false);
    itemBoxSizer4->Add(itemStaticText5, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    m_titleCtrl = new wxTextCtrl( itemPanel1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(200, -1), wxTE_READONLY );
    itemBoxSizer4->Add(m_titleCtrl, 1, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer7 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer3->Add(itemBoxSizer7, 1, wxGROW, 5);

    wxBoxSizer* itemBoxSizer8 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer7->Add(itemBoxSizer8, 1, wxGROW, 5);

    wxStaticText* itemStaticText9 = new wxStaticText( itemPanel1, wxID_STATIC, _("&Menu:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer8->Add(itemStaticText9, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);

    m_menuCtrl = new AxMenuListCtrl( itemPanel1, wxID_ANY, wxDefaultPosition, wxSize(200, 100), wxLC_REPORT|wxLC_NO_HEADER|wxLC_SINGLE_SEL|AxUIUtilities::GetSuitableThemedBorderStyle() );
    itemBoxSizer8->Add(m_menuCtrl, 1, wxGROW|wxALL, 5);

    wxBoxSizer* itemBoxSizer11 = new wxBoxSizer(wxHORIZONTAL);
    itemBoxSizer8->Add(itemBoxSizer11, 0, wxALIGN_CENTER_HORIZONTAL, 5);

    wxButton* itemButton12 = new wxButton( itemPanel1, ID_UP, _("&Up"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer11->Add(itemButton12, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxButton* itemButton13 = new wxButton( itemPanel1, ID_HOME, _("&Home"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer11->Add(itemButton13, 0, wxALIGN_CENTER_VERTICAL|wxALL, 5);

    wxBoxSizer* itemBoxSizer14 = new wxBoxSizer(wxVERTICAL);
    itemBoxSizer7->Add(itemBoxSizer14, 2, wxGROW, 5);

    wxStaticText* itemStaticText15 = new wxStaticText( itemPanel1, wxID_STATIC, _("&View:"), wxDefaultPosition, wxDefaultSize, 0 );
    itemBoxSizer14->Add(itemStaticText15, 0, wxALIGN_LEFT|wxLEFT|wxRIGHT|wxTOP, 5);

    m_pagerCtrl = new AxPagerCtrl( itemPanel1, wxID_ANY, wxDefaultPosition, wxSize(200, -1), wxTAB_TRAVERSAL|AxUIUtilities::GetSuitableThemedBorderStyle() );
    itemBoxSizer14->Add(m_pagerCtrl, 1, wxGROW|wxALL, 5);

    m_descriptionCtrl = new wxTextCtrl( itemPanel1, wxID_ANY, wxEmptyString, wxDefaultPosition, wxSize(-1, 60), wxTE_MULTILINE|wxTE_READONLY );
    itemBoxSizer3->Add(m_descriptionCtrl, 0, wxGROW|wxALL, 5);

////@end AxContainerCtrl content construction

    m_wakeUpIdleTimer.Start(100);

    wxPanel* frontPage = new wxPanel(m_pagerCtrl, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNO_BORDER);
    frontPage->SetBackgroundColour(*wxWHITE);
    m_pagerCtrl->AddPage(frontPage, wxT("Front"), true);
}


/*
 * Should we show tooltips?
 */

bool AxContainerCtrl::ShowToolTips()
{
    return true;
}

/*
 * Get bitmap resources
 */

wxBitmap AxContainerCtrl::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin AxContainerCtrl bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end AxContainerCtrl bitmap retrieval
}

/*
 * Get icon resources
 */

wxIcon AxContainerCtrl::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin AxContainerCtrl icon retrieval
    wxUnusedVar(name);
    return wxNullIcon;
////@end AxContainerCtrl icon retrieval
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_UP
 */

void AxContainerCtrl::OnUpClick( wxCommandEvent& WXUNUSED(event) )
{
    if (m_controller)
        m_controller->GoUp();
}


/*
 * wxEVT_UPDATE_UI event handler for ID_UP
 */

void AxContainerCtrl::OnUpUpdate( wxUpdateUIEvent& event )
{
    if (m_controller)
        event.Enable(m_controller->CanGoUp());
    else
        event.Enable(false);
}


/*
 * wxEVT_COMMAND_BUTTON_CLICKED event handler for ID_HOME
 */

void AxContainerCtrl::OnHomeClick( wxCommandEvent& WXUNUSED(event) )
{
    if (m_controller)
        m_controller->GoHome();
}


/*
 * wxEVT_UPDATE_UI event handler for ID_HOME
 */

void AxContainerCtrl::OnHomeUpdate( wxUpdateUIEvent& event )
{
    if (m_controller)
        event.Enable(m_controller->CanGoHome());
    else
        event.Enable(false);
}


/*
 * wxEVT_COMMAND_LIST_KEY_DOWN event handler for wxID_ANY
 */

void AxContainerCtrl::OnAnyKeyDown( wxListEvent& event )
{
    if (event.GetKeyCode() == WXK_ESCAPE)
    {
        if (m_controller)
            m_controller->GoUp();
    }
    else
        event.Skip();
}


/*
 * wxEVT_IDLE event handler for ID_AXCONTAINERCTRL
 */

void AxContainerCtrl::OnIdle( wxIdleEvent& event )
{
    if (m_controller && m_controller->GetViewManager())
        m_controller->GetViewManager()->IdleProcessing();

    event.Skip();
}

// Layout the container in response to possible size changes
void AxContainerCtrl::OnSysColourChanged(wxSysColourChangedEvent& event)
{
    Layout();

    event.Skip();
}

void AxContainerCtrlWakeUpIdleTimer::Notify()
{
    wxWakeUpIdle();
}
